package com.eternal.framework.component;

import android.app.Activity;
import androidx.lifecycle.ViewModelProviders;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.eternal.framework.bus.Messenger;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @author li
 */
public abstract class BaseFragment<V extends ViewDataBinding, VM extends BaseViewModel> extends BaseRxFragment implements IActive {
    protected V binding;
    protected VM viewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, initContentView(inflater, container, savedInstanceState), container, false);
        return binding.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //解除Messenger注册
        Messenger.getDefault().unregister(viewModel);
        if (viewModel != null) {
            viewModel.unregisterRxBus();
            getLifecycle().removeObserver(viewModel);
        }
        if (binding != null) {
            binding.unbind();
        }

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //私有的初始化DataBinding和ViewModel方法
        initViewDataBinding();
        //注册RxBus
        viewModel.registerRxBus();
    }

    /**
     * 注入绑定
     */
    private void initViewDataBinding() {
        int viewModelId = initVariableId();
        viewModel = initViewModel();
        if (viewModel == null) {
            Class modelClass;
            Type type = getClass().getGenericSuperclass();
            if (type instanceof ParameterizedType) {
                modelClass = (Class) ((ParameterizedType) type).getActualTypeArguments()[1];
            } else {
                //如果没有指定泛型参数，则默认使用BaseViewModel
                modelClass = BaseViewModel.class;
            }
            viewModel = (VM) ViewModelProviders.of(this).get(modelClass);;
        }
        binding.setVariable(viewModelId, viewModel);
        //支持LiveData绑定xml，数据改变，UI自动会更新
        binding.setLifecycleOwner(this);
        //让ViewModel拥有View的生命周期感应
        getLifecycle().addObserver(viewModel);
        //注入RxLifecycle生命周期
        viewModel.injectLifecycleProvider(this);
        viewModel.initActive(this);
    }



    @Override
    public Intent createIntent(Class<?> clz) {
        return new Intent(getContext(), clz);
    }

    @Override
    public void finish() {
        Activity activity=getActivity();
        if(activity!=null){
            activity.finish();
        }
    }

    @Override
    public void onBackPressed() {
        Activity activity=getActivity();
        if(activity!=null){
            activity.onBackPressed();
        }
    }

    /**
     * 初始化根布局
     * @param inflater inflater
     * @param container layout
     * @param savedInstanceState 数据
     * @return 布局layout的id
     */
    public abstract int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState);

    /**
     * 返回model id
     * @return id
     */
    public abstract int initVariableId();

    /**
     * 初始化ViewModel
     *
     * @return 继承BaseViewModel的ViewModel
     */
    public VM initViewModel() {
        return null;
    }

    public boolean isBackPressed() {
        return false;
    }
}
